package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: hm
 * @Date: 2025/10/06 22:21
 * @Description:
 */
@Service
@SuppressWarnings("all")
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Course> getBySubject(Integer subject) {
        return courseMapper.selectBySubject(subject);
    }

    @Override
    public List<Course> queryAllCourses() {
        return courseMapper.selectAllCourses();
    }

    @Override
    public void update(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    @Override
    public Course getById(Integer id) {
        return courseMapper.selectById(id);
    }

    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    @Override
    public PageResult<Course> page(CourseDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<Course> page = courseMapper.selectByPage(dto);
        Page<Course> result = (Page<Course>) page;
        return PageResult.<Course>builder()
                .total(result.getTotal())
                .rows(result.getResult())
                .build();
    }

    @Override
    public void addCourse(Course course) {
        boolean hasNull = BeanUtil.hasNullField(course, "id", "description", "createTime", "updateTime");
        if (hasNull) {
            throw new RuntimeException("参数错误");
        }
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }
}
